#include "userms.h"
#include "ui_userms.h"

#include <QSqlQuery>
#include <QSqlRecord>
#include <QDebug>

UserMS::UserMS(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserMS)
{
    ui->setupUi(this);
}

UserMS::~UserMS()
{
    delete ui;
}

void UserMS::SqlSearchuserinfos(QString sql)
{
    QSqlQuery query;
    qDebug() << sql;
    if(query.exec(sql) == true)
    {
        int id_idx = query.record().indexOf("id");
        int name_idx = query.record().indexOf("name");
        int pswd_idx = query.record().indexOf("pswd");
        int tel_idx = query.record().indexOf("tel");
        int head_idx = query.record().indexOf("head");

        while(query.next())
        {
            QString id = query.value(id_idx).toString();
            QString name = query.value(name_idx).toString();
            QString pswd = query.value(pswd_idx).toString();
            QString tel = query.value(tel_idx).toString();
            QString head = query.value(head_idx).toString();

            UserInfo info(id,name,pswd,tel,head);
            m_list.append(info);
        }
    }
    else
    {
        qDebug() << "exec error!";
    }

    updateTableInfos();
}

void UserMS::updateTableInfos()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setRowCount(m_list.length());

    // set header labels
    QStringList headers;
    headers << "编号" << "姓名" << "密码" << "电话" << "头像";
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    for(int i = 0;i < m_list.count();i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem(m_list.at(i).getID());
        ui->tableWidget->setItem(i,0,item);
        item = new QTableWidgetItem(m_list.at(i).getName());
        ui->tableWidget->setItem(i,1,item);
        item = new QTableWidgetItem(m_list.at(i).getPswd());
        ui->tableWidget->setItem(i,2,item);
        item = new QTableWidgetItem(m_list.at(i).getTel());
        ui->tableWidget->setItem(i,3,item);
        item = new QTableWidgetItem(m_list.at(i).getHead());
        ui->tableWidget->setItem(i,4,item);
    }
}

void UserMS::on_cb_condition_currentIndexChanged(int index)
{
    m_searchCond = index;
    if(m_searchCond == 0)
    {
        ui->le_condition->clear();
        ui->le_condition->setEnabled(false);
    }
    else
    {
        ui->le_condition->setEnabled(true);
    }
}

void UserMS::on_pb_search_clicked()
{
    m_list.clear();
    QString queryString;
    if(m_searchCond == Search_ID)
    {
        queryString = QString("select * from user_info where id = '%1'").arg(ui->le_condition->text());
    }
    else if(m_searchCond == Search_Name)
    {
        queryString = QString("select * from user_info where name = '%1'").arg(ui->le_condition->text());
    }
    else if(m_searchCond == Search_Tel)
    {
        queryString = QString("select * from user_info where tel = '%1'").arg(ui->le_condition->text());
    }
    else
    {
        queryString = QString("select * from user_info");
    }

    SqlSearchuserinfos(queryString);
}
